package com.asohofrucol.web;

import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;

import com.asohofrucol.dto.AsohofrucolSessionDTO;
import com.asohofrucol.dto.ProcessTransferInnovationRegisterDTO;
import com.asohofrucol.service.UserOwnerService;

public class ProcessTransferInnovationController {
	@Autowired
	private AsohofrucolSessionDTO aSession;
	@Autowired
	private UserOwnerService userOwnerService;

	public void createNewProductionProcessRegister(ActionEvent actionEvent) {
		if (aSession.getProcessTransferInnovation().getProductionProcess()
				.size() < 2) {
			ProcessTransferInnovationRegisterDTO productionProcess = new ProcessTransferInnovationRegisterDTO();
			productionProcess.setDescription("");
			productionProcess.setSourceOther("");
			productionProcess.setActivity(591L);// codigo de production
			productionProcess.setRuatId(aSession.getSelectedRuatRegister().getId());
			productionProcess.setStatus("1");
			aSession.getProcessTransferInnovation().getProductionProcess()
					.add(productionProcess);
		}
	}


	
	public void createNewTransformationProcessRegister(ActionEvent actionEvent) {
		if (aSession.getProcessTransferInnovation().getTransformationProcess()
				.size() < 2) {
			ProcessTransferInnovationRegisterDTO transformationProcess = new ProcessTransferInnovationRegisterDTO();
			transformationProcess.setDescription("");
			transformationProcess.setSourceOther("");
			transformationProcess.setActivity(594L);// codigo de transformacion
			transformationProcess.setRuatId(aSession.getSelectedRuatRegister().getId());
			transformationProcess.setStatus("1");
			aSession.getProcessTransferInnovation().getTransformationProcess()
					.add(transformationProcess);
		}
	}

	public void createNewCommercializationProcessRegister(
			ActionEvent actionEvent) {
		if (aSession.getProcessTransferInnovation()
				.getCommercializationProcess().size() < 2) {
			ProcessTransferInnovationRegisterDTO commercializationProcess = new ProcessTransferInnovationRegisterDTO();
			commercializationProcess.setDescription("");
			commercializationProcess.setSourceOther("");
			commercializationProcess.setActivity(597L);// codigo de
														// comercializacion
			commercializationProcess.setRuatId(aSession.getSelectedRuatRegister().getId());
			commercializationProcess.setStatus("1");
			aSession.getProcessTransferInnovation()
					.getCommercializationProcess()
					.add(commercializationProcess);
		}
	}

	public void createNewOrganizationalProcessRegister(ActionEvent actionEvent) {
		if (aSession.getProcessTransferInnovation().getOrganizationalProcess()
				.size() < 2) {
			ProcessTransferInnovationRegisterDTO organizationalProcess = new ProcessTransferInnovationRegisterDTO();
			organizationalProcess.setDescription("");
			organizationalProcess.setSourceOther("");
			organizationalProcess.setActivity(600L); // codigode organizacional
			organizationalProcess.setRuatId(aSession.getSelectedRuatRegister().getId());
			organizationalProcess.setStatus("1");
			aSession.getProcessTransferInnovation().getOrganizationalProcess()
					.add(organizationalProcess);
		}
	}
	/**
	 * Este metodo quita de la lista el elemento sobre el que se hace clic
	 * 
	 * @param actionEvent
	 */
	public void deleteProductionProcessRegister(ActionEvent actionEvent) {
		ProcessTransferInnovationRegisterDTO selected = (ProcessTransferInnovationRegisterDTO) actionEvent
				.getComponent().getAttributes().get("register");
		aSession.getProcessTransferInnovation().getProductionProcess()
				.remove(selected);
		if(selected.getId()!=null){
			String userModifier = aSession.getUserInfo().getUserName(); 
			userOwnerService.deleteProcessTransferInnovation(selected, userModifier);
		}
	}
	
	public void deleteTransformationProcessRegister(ActionEvent actionEvent){
		ProcessTransferInnovationRegisterDTO selected = (ProcessTransferInnovationRegisterDTO) actionEvent
				.getComponent().getAttributes().get("register");
		aSession.getProcessTransferInnovation().getTransformationProcess()
				.remove(selected);
		if(selected.getId()!=null){
			String userModifier = aSession.getUserInfo().getUserName(); 
			userOwnerService.deleteProcessTransferInnovation(selected, userModifier);
		}		
	}
	
	public void deleteCommercializationProcessRegister(ActionEvent actionEvent){
		ProcessTransferInnovationRegisterDTO selected = (ProcessTransferInnovationRegisterDTO) actionEvent
				.getComponent().getAttributes().get("register");
		aSession.getProcessTransferInnovation().getCommercializationProcess()
				.remove(selected);
		if(selected.getId()!=null){
			String userModifier = aSession.getUserInfo().getUserName(); 
			userOwnerService.deleteProcessTransferInnovation(selected, userModifier);
		}		
	}

	public void deleteOrganizationalProcessRegister(ActionEvent actionEvent){
		ProcessTransferInnovationRegisterDTO selected = (ProcessTransferInnovationRegisterDTO) actionEvent
				.getComponent().getAttributes().get("register");
		aSession.getProcessTransferInnovation().getOrganizationalProcess()
				.remove(selected);
		if(selected.getId()!=null){
			String userModifier = aSession.getUserInfo().getUserName(); 
			userOwnerService.deleteProcessTransferInnovation(selected, userModifier);
		}		
	}
}
